package com.ets.controller;

import javax.annotation.Resource;
import javax.servlet.http.*;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.ets.entity.*;
import com.ets.service.*;
import com.ets.util.*;
import java.util.*;

@Controller
@RequestMapping("manage")
public class UpdateController {
	@Resource
	private LessonService lService;
	@Resource
	private PaperService pService;
	@Resource
	private QuestionService qService;
	
	//Add new lesson or edit lesson.
	@RequestMapping("addLesson")
	public String addLesson(){
		return "manage/addLesson";
	}
	
	//Edit existed lesson.
	@RequestMapping("editLesson")
	public String editLesson(@RequestParam("id") int id,ModelMap model){
		Lesson lesson=lService.getLesson(id);
		model.put("lesson", lesson);
		return "manage/addLesson";
	}
	
	//Save new lesson or update old lesson.
	@RequestMapping("saveLesson")
	public String saveLesson(HttpServletRequest req,Lesson lesson){
		lesson.setJoinTime(TimeUtil.getTime());
		lService.updateLesson(lesson);
	
		return "redirect:lesson.do";
	}
	
	//Add new paper.
	@RequestMapping("addPaper")
	public String addPaper(ModelMap model){
		List<Lesson> lessons=lService.getAllLessons();
		model.put("lessons", lessons);
		model.put("page", "addPaper.jsp");
		return "manage/main";
	}
	
	//Save new paper.
	@RequestMapping("savaPaper")
	public String savaPaper(HttpServletRequest req){
		String pId=req.getParameter("id");
		String name=req.getParameter("name");
		System.out.println("save paper:"+pId+"#"+name);
		Paper paper=null;

		if(pId==null||"".equals(pId))
			paper=new Paper();
	
		else		
			paper=pService.getPaper(Integer.parseInt(pId));		
		
		//Get paper's lesson id.
		int lId=Integer.parseInt(req.getParameter("lId"));
		Lesson lesson=lService.getLesson(lId);
		
		//Set parameters of paper.
		paper.setName(name);
		paper.setLesson(lesson);
		paper.setJoinTime(TimeUtil.getTime());
		pService.updatePaper(paper);
		
		return "redirect:paper.do";
	}
	
	//Edit paper.
	@RequestMapping("editPaper")
	public String editPaper(@RequestParam("id") int id,ModelMap model){
		Paper paper=pService.getPaper(id);
		
		List<Lesson> lessons=lService.getAllLessons();
		model.put("lessons", lessons);
		model.put("page", "addPaper.jsp");
		model.put("paper", paper);
		return "manage/main";
	}
	
	//Add new question.
	@RequestMapping("addQuestion")
	public String addQuestion(ModelMap model,HttpServletRequest req){
		String lId=req.getParameter("lId");
		List<Lesson> lessons=lService.getAllLessons();
		Lesson lesson=null;
		if(lId!=null)
			lesson=lService.getLesson(Integer.parseInt(lId));
		else
			lesson=lessons.get(0);
		
		model.put("lessons", lessons);
		model.put("lesson", lesson);
		model.put("page", "addQuestion.jsp");
	
		return "manage/main";
	}
	
	//Edit question.
	@RequestMapping("editQuestion")
	public String editQuestion(ModelMap model,HttpServletRequest req){
		String lId=req.getParameter("lId");
		Question question=qService.getQuestion(
			Integer.parseInt(req.getParameter("id")));
		List<Lesson> lessons=lService.getAllLessons();
		Lesson lesson=null;
		if(lId!=null)
			lesson=lService.getLesson(Integer.parseInt(lId));
		else
			lesson=question.getPaper().getLesson();

		model.put("lessons", lessons);
		model.put("lesson", lesson);
		req.getSession().setAttribute("question", question);
		model.put("page", "editQuestion.jsp");
		
		return "manage/main";
	}
	
	//Save question.
	@RequestMapping("saveQuestion")
	public String saveQuestion(HttpServletRequest req){
		String pId=req.getParameter("pId");
		Paper paper=pService.getPaper(Integer.parseInt(pId));
		String ans=null;
		String type=req.getParameter("type");
		if("s".equals(type)){
			ans=req.getParameter("answer");
		}
		else{
			String[] answers=req.getParameterValues("answers");
			ans=AnswerUtil.parseMultipleAnswer(answers);
		}
		
		String subject=req.getParameter("subject");
		String optionA=req.getParameter("optionA");
		String optionB=req.getParameter("optionB");
		String optionC=req.getParameter("optionC");
		String optionD=req.getParameter("optionD"); 
		
		Question q=new Question();
		
		q.setPaper(paper);
		q.setType(type);
		q.setSubject(subject);
		q.setOptionA(optionA);
		q.setOptionB(optionB);
		q.setOptionC(optionC);
		q.setOptionD(optionD);
		q.setAnswer(ans);
		q.setJoinTime(TimeUtil.getTime());
		
		qService.save(q);
		
		return "redirect:question.do";
	}

	//Update Question
	@RequestMapping("updateQuestion")
	public String updateQuestion(HttpServletRequest req){
		String qId=req.getParameter("qId");
		System.out.println(qId);
		String subject=req.getParameter("subject");
		String optionA=req.getParameter("optionA");
		String optionB=req.getParameter("optionB");
		String optionC=req.getParameter("optionC");
		String optionD=req.getParameter("optionD"); 
		String answer=req.getParameter("answer");
		
		//Set properties of question.
		Question q=qService.getQuestion(Integer.parseInt(qId));
		q.setSubject(subject);
		q.setOptionA(optionA);
		q.setOptionB(optionB);
		q.setOptionC(optionC);
		q.setOptionD(optionD);
		q.setAnswer(answer);
		qService.save(q);
		return "redirect:question.do";
	}
}
